package com.aaie.gru.resource.controller;


import com.aaie.gru.resource.entity.GruSysResource;
import com.aaie.gru.resource.entity.GruSysResourceVo;
import com.aaie.gru.resource.entity.GruSysRoleResourceMap;
import com.aaie.gru.resource.service.IGruSysResourceService;
import com.aaie.gru.resource.service.IGruSysRoleResourceMapService;
import com.aaie.gru.response.ResMessage;
import com.aaie.gru.role.entity.GruSysRoleUserMap;
import com.aaie.gru.role.service.IGruSysRoleUserMapService;
import com.aaie.gru.util.CommonUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色资源映射表 前端控制器
 * </p>
 *
 * @author lvxing
 * @since 2019-08-18
 */
@RestController
@RequestMapping("/resource")
public class GruSysRoleResourceMapController {

    @Autowired
    IGruSysRoleResourceMapService gruSysRoleResourceMapService;

    @Autowired
    IGruSysResourceService gruSysResourceService;

    @Autowired
    IGruSysRoleUserMapService gruSysRoleUserMapService;

    @GetMapping("/getResourceListByUserId")
    public String getResourceListByUserId(HttpSession session) {
        // 按用户获取角色列表
        List<String> roleIdList = new ArrayList<>();
        QueryWrapper<GruSysRoleUserMap> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", CommonUtil.getUser(session).getUserId());
        List<GruSysRoleUserMap> list = gruSysRoleUserMapService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(list)) {
            for (GruSysRoleUserMap gruSysRoleUserMap : list) {
                roleIdList.add(gruSysRoleUserMap.getRoleId());
            }
        }
        // 按角色列表获取角色资源映射列表
        QueryWrapper<GruSysRoleResourceMap> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("role_id", roleIdList);
        List<String> resourceIdList = new ArrayList<>();
        List<GruSysRoleResourceMap> list1 = gruSysRoleResourceMapService.list(queryWrapper1);
        if (!CollectionUtils.isEmpty(list1)) {
            for (GruSysRoleResourceMap gruSysRoleResourceMap : list1) {
                resourceIdList.add(gruSysRoleResourceMap.getResourceId());
            }
        }
        // 获取资源列表
        QueryWrapper<GruSysResource> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.in("resource_id", resourceIdList);
        List<GruSysResource> list2 = gruSysResourceService.list(queryWrapper2);
        // 返回资源列表
        return ResMessage.ok(list2);
    }

    @GetMapping("/getCheckResourceListByUserId")
    public String getCheckResourceListByUserId(HttpSession session) {
        // 按用户获取角色列表
        List<String> roleIdList = new ArrayList<>();
        QueryWrapper<GruSysRoleUserMap> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", CommonUtil.getUser(session).getUserId());
        List<GruSysRoleUserMap> list = gruSysRoleUserMapService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(list)) {
            for (GruSysRoleUserMap gruSysRoleUserMap : list) {
                roleIdList.add(gruSysRoleUserMap.getRoleId());
            }
        }
        // 按角色列表获取角色资源映射列表
        QueryWrapper<GruSysRoleResourceMap> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("role_id", roleIdList);
        List<String> resourceIdList = new ArrayList<>();
        List<GruSysRoleResourceMap> list1 = gruSysRoleResourceMapService.list(queryWrapper1);
        if (!CollectionUtils.isEmpty(list1)) {
            for (GruSysRoleResourceMap gruSysRoleResourceMap : list1) {
                resourceIdList.add(gruSysRoleResourceMap.getResourceId());
            }
        }
        // 获取资源列表
        QueryWrapper<GruSysResource> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.in("resource_id", resourceIdList);
        List<GruSysResource> list2 = gruSysResourceService.list(queryWrapper2);
        // 获取全部资源列表
        List<GruSysResource> gruSysResourceList = gruSysResourceService.list();
        // 返回资源列表
        List<GruSysResourceVo> resultList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list2)) {
            for (GruSysResource gruSysResource : gruSysResourceList) {
                GruSysResourceVo gruSysResourceVo = new GruSysResourceVo();
                BeanUtils.copyProperties(gruSysResource, gruSysResourceVo);
                gruSysResourceVo.setCheck(false);
                for (GruSysResource temp : list2) {
                    if (gruSysResource.getResourceId().equals(temp.getResourceId())) {
                        gruSysResourceVo.setCheck(true);
                        break;
                    }
                }
                resultList.add(gruSysResourceVo);
            }
        }
        return ResMessage.ok(resultList);
    }
}
